package com.software.fanfan.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 对各类参数进行校验
 *
 * Created by shiyanfei on 2018-01-09.
 */
public class CheckUtil {

    /**
     * 正则验证
     * @param string
     * @param reg
     * @return
     */
    private static  boolean check(String string, String reg) {
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(string);
        boolean flag = m.matches();
        return flag;
    }

    /**
     * 验证手机号格式
     * @param string
     * @return
     */
    public static boolean checkMobile(String string) {
        String reg = "^[1][3-9][0-9]{9}$";
        return check(string, reg);
    }

    /**
     * 验证座机号格式
     */
    public static boolean checkTel(String string){
        String reg="^0\\d{2,3}-[1-9]\\d{6,7}$";
        return check(string,reg);
    }

    /**
     * 验证身份证号格式
     * @param string
     * @return
     */
    public static boolean checkIdCard(String string) {
        String reg = "^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
        return check(string, reg);
    }

    /**
     * 验证一个字符串是不是纯数字
     * @param string
     * @return
     */
    public static boolean checkNumber(String string) {
        String reg = "^[0-9]+(.[0-9]{1,2})?$";
        return check(string, reg);
    }

    /**
     * 验证一个字符串是不是正整数
     * @param string
     * @return
     */
    public static boolean checkNumberToInteger(Object string) {
        String reg = "^[1-9]*[1-9][0-9]*$";
        return check(String.valueOf(string), reg);
    }

    /**
     * 验证一个字符串数组是否为空
     * @param string
     * @return
     */
    public static boolean StringArrIsNotEmpty(Object[] string) {
        if(string!=null){
            boolean flag = true;
            for(Object o :string){
                if(o.equals("")){
                    flag = false;
                    break;
                }
            }
            return flag;
        }else{
            return false;
        }
    }

}
